// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import "babel-polyfill"; // 支持IE执行原生script
import Vue from "vue";
// import Vue from 'vue/dist/vue.common.js';
import VueAMap from "vue-amap";
import App from "./App";
import router from "./router";
import store from "./store";
import ElementUI from "element-ui";

import "assets/iconfont/iconfont"; // iconfont 具体图标见https://github.com/PanJiaChen/vue-element-admin/wiki
import * as filters from "./filters"; // 全局vue filter
import Multiselect from "vue-multiselect"; // 使用的一个多选框组件，element-ui的select不能满足所有需求

import Sticky from "components/Sticky"; // 粘性header组件
import IconSvg from "components/Icon-svg"; // svg 组件
import vueWaves from "./directive/waves"; // 水波纹指令
import errLog from "store/errLog"; // error log组件
// import '../src/assets/icons/index';//尝试svg插件封装
// import './mock/index.js';  // 该项目所有请求使用mockjs模拟
import { getToken } from "utils/auth";
import { dictionariespluscollection } from "./views/components/FLPage/dictionariespluscollection.vue";
import echarts from "echarts";
import VueBus from "vue-bus";
// 引入vxe-table
import "xe-utils";
import "./plugins/table";
import bmap from "vue-baidu-map";
import Print from "vue-print-nb";
// import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import "vue-multiselect/dist/vue-multiselect.min.css"; // 多选框组件css
import "element-ui/lib/theme-chalk/index.css";
// import "@/style/reset.css";
import "assets/custom-theme/index.css"; // 换肤版本element-ui css
import "assets/custom-theme/schedule/index.css"; // 生产调度换肤版本element-ui css
import NProgress from "nprogress"; // Progress 进度条
import "nprogress/nprogress.css"; // Progress 进度条 样式
import "normalize.css/normalize.css"; // normalize.css 样式格式化
//拖曳
import "vue-draggable-resizable/src/components/vue-draggable-resizable.css";

Vue.use(bmap, {
    ak: "gVI1IQGAwYrewAhC1XtAx9lfydbPkYoS",
});
Vue.use(VueBus);
Vue.use(echarts);
Vue.prototype.$echarts = echarts;
// register globally
Vue.component("multiselect", Multiselect);
Vue.component("Sticky", Sticky);
Vue.component("icon-svg", IconSvg);
Vue.component("dictionariespluscollection", dictionariespluscollection);
Vue.use(ElementUI);
Vue.use(vueWaves);
Vue.use(Print);
// Vue.use(UmyUi);
//地图
setTimeout(() => {
    localStorage.clear();
    Vue.use(VueAMap);
}, 0);
VueAMap.initAMapApiLoader({
    key: "84d2a912eebb168b38bd0f9d2533fe6e",
    plugin: [
        "AMap.Autocomplete",
        "AMap.PlaceSearch",
        "AMap.Scale",
        "AMap.OverView",
        "AMap.ToolBar",
        "AMap.MapType",
        "AMap.PolyEditor",
        "AMap.CircleEditor",
        "AMap.Geocoder",
    ],
    // 默认高德 sdk 版本为 1.4.4
    v: "1.4.4",
});

// register global utility filters.
Object.keys(filters).forEach((key) => {
    Vue.filter(key, filters[key]);
});

// register global progress.
const whiteList = [
    "/login",
    "/authredirect",
    "/userInstallApproval",
    "/ycUserInstallApproval", // 特困户-
    "/ycResidentUserInstallApproval", // 居民
    "/ycNonResidentUserInstallApproval", // 非居民
    "/ycWatersupplyUserInstallApproval", // 供水
    "/usersInstallApproval",
    "/userOpenAccountApproval",
    "/usersOpenAccountApproval",
    "/userPhoneSearch",
    "/usersPhoneSearch",
    "/userPhoneSearchInstall",
    "/userInstallApprovalAlter",
    "/usersInstallApprovalAlter",
    "/userOpenAccountApprovalAlter",
    "/usersOpenAccountApprovalAlter",
    "/userEvaluate",
    "/yichengBill",
    "/yichengLogin",
]; // 不重定向白名单
router.beforeEach((to, from, next) => {
    // console.log('路由拦截！路由拦截！路由拦截！路由拦截！路由拦截！');
    // console.log(store.state.user.menus);
    NProgress.start(); // 开启Progress
    // console.log('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFf')
    if (getToken()) {
        // 判断是否有token
        if (to.path === "/login") {
            next({
                path: "/",
            });
        } else {
            if (store.getters.menus === undefined) {
                // 判断当前用户是否已拉取完user_info信息
                store
                    .dispatch("GetInfo")
                    .then((info) => {
                        // 拉取user_info
                        const menus = {};
                        for (let i = 0; i < info.menus.length; i++) {
                            menus[info.menus[i].code] = true;
                        }
                        console.log("用户菜单的结果-->", menus);
                        store.dispatch("GenerateRoutes", menus).then(() => {
                            // 生成可访问的路由表
                            console.log("开始添加路由表==", store.getters.addRouters);
                            router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
                            next({
                                ...to,
                            }); // hack方法 确保addRoutes已完成
                        });
                    })
                    .catch(() => {
                        store.dispatch("FedLogOut").then(() => {
                            next({
                                path: "/login",
                            });
                        });
                    });
            } else {
                //  else {
                //   // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
                //   if (hasPermission(store.getters.roles, to.meta.role)) {
                //     next();//
                //   } else {
                next();
                //   }
                //   // 可删 ↑
            }
        }
    } else {
        // console.log('在免登录白名单，直接进入..........................................')
        if (whiteList.indexOf(to.path) !== -1) {
            // 在免登录白名单，直接进入
            next();
        } else {
            next("/login"); // 否则全部重定向到登录页
            NProgress.done(); // 在hash模式下 改变手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps：history模式下无问题，可删除该行！
        }
    }
});

router.afterEach(() => {
    NProgress.done(); // 结束Progress
});

// 阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false;

// 生产环境错误日志
if (process.env.NODE_ENV === "production") {
    Vue.config.errorHandler = function(err, vm) {
        // console.log(err, window.location.href);
        errLog.pushLog({
            err,
            url: window.location.href,
            vm,
        });
    };
}

new Vue({
    router,
    store,
    render: (h) => h(App),
}).$mount("#app");